<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索订单 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入订单编号" v-model="queryInfo.query" clearable @clear="getOrderList()" disabled>
            <el-button slot="append" icon="el-icon-search" @click="searchOrder" disabled></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表数据 -->
      <el-row>
        <el-table :data="orderList" style="width: 100%" stripe border>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="order_number" label="订单编号" width="180">
            <template slot-scope="scope">
              {{ scope.row.order_number.substr(7) }}
            </template>
          </el-table-column>
          <el-table-column prop="order_price" label="订单价格" width="180">
          </el-table-column>
          <el-table-column label="是否付款">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.order_pay === '1'"
                >已付款</el-tag
              >
              <el-tag type="danger" v-else>未付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="is_send" label="是否发货"></el-table-column>
          <el-table-column label="下单时间">
            <template slot-scope="scope">
              {{ scope.row.create_time | dataFormat }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                circle
                size="mini"
                icon="el-icon-edit"
                @click="showEditAddress"
              ></el-button>
              <el-button
                type="success"
                circle
                size="mini"
                icon="el-icon-location"
                @click="showProgress(scope.row.order_number)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 订单地址修改弹出框 -->
    <el-dialog
      title="修改收货地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            expand-trigger="hover"
            :options="citydata"
            v-model="addressForm.address1"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 订单物流信息弹出框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressVisible"
      width="50%"
    >
      <i class="el-icon-error" style="color: red;"></i>
      <span> 很遗憾，此功能未完成, 无法查看订单物流信息!</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入地区数据
import citydata from './citydata'
export default {
  data() {
    return {
      // 订单列表请求参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 订单列表总数
      total: null,
      // 订单列表数据
      orderList: [],
      // 保存地区数据
      citydata,
      addressVisible: false,
      // 订单地址修改表单数据
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      progressVisible: false
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.https.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('订单列表获取失败!')
      }
      this.total = res.data.total
      this.orderList = res.data.goods
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getOrderList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getOrderList()
    },
    showEditAddress() {
      this.addressVisible = true
    },
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    async showProgress(id) {
      // 后台接口问题，暂无法解决，此功能跳过
      // const { data: res } = await this.https.get('/kuaidi/804909574412544580')
      this.progressVisible = true
    },
    searchOrder() {
      // 无法通过 order_number 查找订单数据
      // const str = 'itcast-'
      // this.queryInfo.query = str + this.queryInfo.query
      // this.getOrderList()
    }
  },
  created() {
    this.getOrderList()
  }
}
</script>

<style lang="less">
.el-cascader {
  width: 100%;
}
</style>
